package com.littlesandbox.scale_cal;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.RadioButton;
import android.widget.LinearLayout;
import java.util.ArrayList;

//市场营销计算器
public class MarketCal extends Activity
{ 
    public RadioGroup cal_method;
    public EditText food_spend_view;
    public EditText all_spend_view;
    public TextView market_result;
    public RadioButton engeer;
    public RadioButton market_need;
    public String calcuate_method="";
    //价格需求弹性
    public EditText p0;
    public EditText p1;
    public EditText q0;
    public EditText q1;
    //计算参数编辑框的父布局 
    public  LinearLayout enge_view;
    public LinearLayout market_need_root;
    public LinearLayout bounce_root;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_market);

        cal_method=findViewById(R.id.cal_method);
        food_spend_view=findViewById(R.id.food_spend);
        all_spend_view=findViewById(R.id.all_spend);
        market_result=findViewById(R.id.market_result);
        enge_view=findViewById(R.id.enge_view);
        //价格需求弹性
        p0=findViewById(R.id.p0);
        p1=findViewById(R.id.p1);
        q0=findViewById(R.id.q0);
        q1=findViewById(R.id.q1);
        //市场需求编辑框父节点
        market_need_root=findViewById(R.id.market_need_root);
        engeer=findViewById(R.id.enger);
        //市场需求单选
        market_need=findViewById(R.id.market_need);
        //公式说明
        final TextView info = findViewById(R.id.info);
        //价格需求弹性 
        final RadioButton price_bounce = findViewById(R.id.rb_price_bounce);
        //价格需求弹性参数编辑框父节点
        bounce_root=findViewById(R.id.bounce_root);
        cal_method.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(RadioGroup p1,int check_id)
                {
                    if(check_id==engeer.getId())
                    {
                        calcuate_method="恩格尔系数";
                        info.setText("恩格尔系数:食品支出/总收入,系数越大越贫困,越小,越有钱");
                        enge_view.setVisibility(View.VISIBLE);
                        market_need_root.setVisibility(View.GONE);
                        bounce_root.setVisibility(View.GONE);
                    }
                    if(check_id==market_need.getId())
                    {
                        calcuate_method="市场需求总量";
                        info.setText("市场需求总量=总人数x销量x单价");
                        enge_view.setVisibility(View.GONE);
                        bounce_root.setVisibility(View.GONE);
                        market_need_root.setVisibility(View.VISIBLE);
                    }
                    if(check_id==price_bounce.getId())
                    {
                        info.setText("(p0-p1)/(q0-q1)后的绝对值\n如果结果>1,则价格需求弹性大,价格变化引发销售量的大变化,如果结果=1为标准弹性,如果结果<1,为弹性小");
                        calcuate_method="产品价格需求弹性";
                        bounce_root.setVisibility(View.VISIBLE);
                        enge_view.setVisibility(View.GONE);
                        market_need_root.setVisibility(View.GONE);
                    }
                }
            });
        //   Toast.makeText(this,"food:"+food_spend_view,Toast.LENGTH_LONG).show();
    }
    //计算按钮
    public void cal(View v)
    {
        if(calcuate_method.equals("恩格尔系数"))
        {
            if(food_spend_view.getText().length()!=0&&all_spend_view.getText().length()!=0)
            {
                double food =Double.valueOf(food_spend_view.getText().toString());
                double all = Double.valueOf(all_spend_view.getText().toString());
                double result =  Enger(food,all);
                market_result.setText("结果:"+result);
                iapp.tw("食品支出_"+food+"总收入_"+all);
            } else
            {
                iapp.tw(Error.no_text_data);
            }
        }
        if(calcuate_method.equals("市场需求总量"))
        {
            EditText v1 = (EditText)market_need_root.getChildAt(0);
            EditText v2 =(EditText) market_need_root.getChildAt(1);
            EditText v3 = (EditText) market_need_root.getChildAt(2);
            if(v1.getText().length()!=0&&v2.getText().length()!=0&&v3.getText().length()!=0)
            {
                int d1 = Integer.valueOf(v1.getText().toString());
                int d2 = Integer.valueOf(v2.getText().toString());
                double d3 = Double.valueOf(v3.getText().toString());
                double result = total_need(d1,d2,d3);
                market_result.setText("结果:"+result);
            } else
            {
                iapp.tw(Error.no_text_data);
            }
        }
        if(calcuate_method.equals("产品价格需求弹性"))
        {
            if(p0.getText().length()!=0&&p1.getText().length()!=0&&q0.getText().length()!=0&&q1.getText().length()!=0)
            {
                float pp0 = Float.valueOf(p0.getText().toString());
                float pp1 = Float.valueOf(p1.getText().toString());
                float qq0 = Float.valueOf(q0.getText().toString());
                float qq1 = Float.valueOf(q1.getText().toString());
                float  result =bounce(pp0,pp1,qq0,qq1);
                result = Math.abs(result);
                market_result.setText("结果:"+result);
                iapp.tw("产品价格需求弹性:"+result);
            } else
            {
                iapp.tw(Error.no_text_data);
            }
        }
    }
    //计算恩格尔系数 
    public double Enger(double pA,double pB)
    {
        return  pA/pB;
    }
    //价格需求弹性 float double精度过高
    public float bounce(float Q0,float Q1,float P0,float P1)
    {
        float result;
        float up =(Q0-Q1)/Q0;
        float down = (P0-P1)/P0;
        result=up/down;
        return result;
    }
    //总需求量 人数 购买数量 单价
    public double total_need(int people,int buys,double sells)
    {
        return people*buys*sells;
    }
}
